prolog './gfold.ijs'
NB. Fold  F. F.. F.: F: F:. F:: -----------------------------------------


NB. monad
10 -: ] F.. + i. 5
10 -: ] F.: + i. 5
2 -: ] F.. - i. 5
2 -: ] F.: - i. 5
1 3 6 10 -: ] F:. + i. 5
7 9 10 10 -: ] F:: + i. 5
1 1 2 2 -: ] F:. - i. 5
_1 3 _2 2 -: ] F:: - i. 5
NB. dyad
110 -: 100 ] F.. + i. 5
110 -: 100 ] F.: + i. 5
_98 -: 100 ] F.. - i. 5
_98 -: 100 ] F.: - i. 5
100 101 103 106 110 -: 100 ] F:. + i. 5
104 107 109 110 110 -: 100 ] F:: + i. 5
_100 101 _99 102 _98 -: 100 ] F:. - i. 5
_96 99 _97 98 _98 -: 100 ] F:: - i. 5
NB. Using v
315 339 381 441 519 -: (6;100 101 102) (+/@:;) F:. ((, 0&{::) ; (+ 1&{::)) i. 5 3
387 447 489 513 519 -: (6;100 101 102) (+/@:;) F:: ((, 0&{::) ; (+ 1&{::)) i. 5 3
NB. early termination
9 21 42 -: (6;100 101 102) (+/@:(0&{::) [ _2 Z: 9 e. 0&{::) F:. ((, 0&{::) ; (+ 1&{::)) i. 5 3
9 21 72 111 -: (6;100 101 102) (+/@:(0&{::) [ _1 Z: 42 = [: +/ 0&{::) F:. ((, 0&{::) ; (+ 1&{::)) i. 5 3
9 21 72 111 -: (6;100 101 102) (+/@:(0&{::) [ 0 Z: 42 = [: +/ 0&{::) F:. ((, 0&{::) ; (+ 1&{::)) i. 5 3
9 21 42 -: (6;100 101 102) (+/@:(0&{::) [ 1 Z: 42 = [: +/ 0&{::) F:. ((, 0&{::) ; (+ 1&{::)) i. 5 3
NB. early termination in u
9 21 42 -: (6;100 101 102) (+/@:(0&{::)) F:. (([ _2 Z: 9 e. 0&{::)@:((, 0&{::) ; (+ 1&{::)))  i. 5 3
9 21 51 90 -: (6;100 101 102) (+/@:(0&{::))  F:. (([ _1 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
9 21 72 111 -: (6;100 101 102) (+/@:(0&{::))  F:. (([ 0 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
9 21 42 -: (6;100 101 102)  (+/@:(0&{::)) F:. (([ 1 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
NB. check uncaught throw
dbr 1
9 21 42 -: (6;100 101 102) (+/@:(0&{::)) F:. (([ _2 Z: 9 e. 0&{::)@:((, 0&{::) ; (+ 1&{::)))  i. 5 3
9 21 51 90 -: (6;100 101 102) (+/@:(0&{::))  F:. (([ _1 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
9 21 72 111 -: (6;100 101 102) (+/@:(0&{::))  F:. (([ 0 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
9 21 42 -: (6;100 101 102)  (+/@:(0&{::)) F:. (([ 1 Z: 42 = [: +/ 0&{::)@((, 0&{::) ; (+ 1&{::))) i. 5 3
dbr Debug
NB. empty results
'no result' -: (6;100 101 102) (+/@:(0&{::) [ _2 Z: 6 e. 0&{::) F:. ((, 0&{::) ; (+ 1&{::)) etx i. 5 3
NB. empty arguments
0 -: ] F.. + i. 0
1 -: ] F.. * i. 0
'domain error' -: ] F:. + etx i. 0
'domain error' -: ] F:. * etx i. 0
0 0 0 -: ] F.. + i. 0 3
1 1 1 -: ] F.. * i. 0 3
'domain error' -: ] F:. + etx i. 0 3
'domain error' -: ] F:. * etx i. 0 3
'domain error' -: ] F.. $ etx i. 0

NB. one item
999 -: +/ F.. (,~) 999
(0$0) -: +/ F:. (,~) 999
999 -: +/ F.. (,~) ,999
(0$0) -: +/ F:. (,~) ,999
999 -: ] F.. (,~) 999
(0$0) -: ] F:. (,~) 999
999 -: ] F.. (,~) ,999
(0$0) -: ] F:. (,~) ,999
9 -: +/ F.. (,~) ,: 2 3 4
(0$0) -: +/ F:. (,~) ,: 2 3 4
2 3 4 -: ] F.. (,~) ,: 2 3 4
(0 3$0) -: ] F:. (,~) ,: 2 3 4

NB. one item - dyad
999 -: 999 +/ F.. (,~) ''
(0$0) -: 999 +/ F:. (,~) ''
999 -: (,999) +/ F.. (,~) ''
(0$0) -: (,999) +/ F:. (,~) ''
999 -: 999 ] F.. (,~) ''
(0$0) -: 999 ] F:. (,~) ''
(,999) -: (,999) ] F.. (,~) ''
(0 1$0) -: (,999) ] F:. (,~) ''
9 -: 2 3 4 +/ F.. (,~) ''
2 3 4 -: 2 3 4 ] F.. (,~) ''
2 3 4 -: 2 3 4 ] F.. (,~) ''
2 3 4 -: 2 3 4 ] F.. (,~) ''
(0$0) -: 2 3 4 +/ F:. (,~) ''
(0$0) -: 2 3 4 +/ F:. (,~) ''
(0 3$0) -: 2 3 4 ] F:. (,~) ''
(0 3$0) -: 2 3 4 ] F:. (,~) ''


NB. F. F:
51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 -: (([   _2 Z: 1 = ])@{:) F: (, -:`(1 3&p.)@.(2&|)@{:) 102
102 51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 -: ([   _2 Z: 1 = {: [ _1 Z: 88 = {:) F. (, -:`(1 3&p.)@.(2&|)@{:) 102
51 154 77 232 116 58 29 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 -: (([   _2 Z: 1 = ] [ _1 Z: 88 = {:)@{:) F: (, -:`(1 3&p.)@.(2&|)@{:) 102
102 51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 -: ([   _2 Z: 1 = {:) F. (([  0 Z: 88 = {:)@(, -:`(1 3&p.)@.(2&|)@{:)) 102
51 154 77 232 116 58 29 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 -: (([   _2 Z: 1 = ])@{:) F: (([  0 Z: 88 = {:)@(, -:`(1 3&p.)@.(2&|)@{:)) 102
102 51 154 77 232 116 58 29 -: ([   0 Z: 88 = {:) F. (([  1 Z: 88 = {:)@(, -:`(1 3&p.)@.(2&|)@{:)) 102
51 154 77 232 116 58 29 -: (([   0 Z: 88 = ])@{:) F: (([  1 Z: 88 = {:)@(, -:`(1 3&p.)@.(2&|)@{:)) 102
949 = # ([   _2 Z: 1 = {:) F. (, -:`(1 3&p.)@.(2&|)@{:) 63728127  NB. many reallocs
NB. empty result
'no result' -: ([   _2 Z: 51 = {:) F: (, -:`(1 3&p.)@.(2&|)@{:) etx 102
'no result' -: ([   _2 Z: 51 = {:) F. (, -:`(1 3&p.)@.(2&|)@{:) etx 102

NB. x Z: y
a =: 3 : 0
t =: t + 1
_3 Z: 1000

0
)
'fold limit' -: ] F. a etx t =: 0
t -: 1000  NB. quit after 1000 tries

NB. Z: y for stats
3 2 1 -: ((Z:@0 , Z:@1 , Z:@2) [ 1 Z: 3 = Z:@0) F.. (_1 Z: 1 >: Z:@0) i. 10   NB. u stops after 3 vs; v skips first u.  Result Z:@2 is before final result added
(2 1 0,:3 2 1) -: ((Z:@0 , Z:@1 , Z:@2) [ 1 Z: 3 = Z:@0) F:. (_1 Z: 1 >: Z:@0) i. 10   NB. same as multiple

NB. inplacing
a1 =: memu a =: 1. + 5 100 100 ?@$ 100  NB. FL so always inplaceable
r =: (+/@, F:: + a)
a1 -: a
r -: (+/@, F:: + memu a)
a1 -: a
3000 > (7!:2 '+/@, F:: + memu a')  -  7!:2 'memu a' 
t1 =: memu t =: {. a
r =: (t1 +/@, F:: + a)
a1 -: a
t1 -: t
r -: ((memu t) +/@, F:: + a)
a1 -: a
t1 -: t
r -: (t +/@, F:: + memu a)
a1 -: a
t1 -: t
r -: ((memu t) +/@, F:: + memu a)
a1 -: a
t1 -: t
3000 > (7!:2 '(memu t) +/@, F:: + memu a')  -  7!:2 '(memu t) [ memu a' 

t =: {{
a=. +/ _2{.y
_2 Z: x<:a
y, a
}}
0 1 1 2 3 5 8 13 21 34 55 89 -: 100 ] F. t 0 1

2 -: 1 [F..(<.@%) 1 2  NB. v doesn't have JTFLGOK

111 -: ((Z: [ 1&Z:)@1@(_1&Z:@(1&~:))) F. ((_1&(33 b.))`(1 3&p.)@.(1&(17 b.))) ^:(>&1)"0 f. 27  NB. Collatz count








epilog''

